package com.whmnx.chitchat.ui.my;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DynamicAdapter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.KeyboardUtils;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.DynamicInputTextMsgDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/8/19 0019 15:38
 * version: 1.0
 * @author Administrator
 */
public class MyFabuActivity extends BaseActivity {
    @BindView(R.id.rl)
    RecyclerView rvDynamic;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;

    @BindView(R.id.tv_msg)
    TextView tvUnMsg;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private int page = 1;
    private int Rows = 10;
    private DynamicAdapter  mDynamicAdapter;
    private List<DynaminBean.ForumListBean> list;
    private DynamicInputTextMsgDialog dynamicInputTextMsgDialog;
    private String forum_id,forumCommentID;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_fragment);
        setTitle("我的发布");
        rlHead.setVisibility(View.GONE);

        tvUnMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UItils.startActivity(ALlMsgActivity.class);
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.FANBU)) {
                getForumList(true);
            }
        }
    }


    @Override
    protected void loadData() {
        initInput();
        rvDynamic.setLayoutManager(new LinearLayoutManager(this));
        rvDynamic.setItemAnimator(new DefaultItemAnimator());
        mDynamicAdapter = new DynamicAdapter(this);
        rvDynamic.setAdapter(mDynamicAdapter);
        getForumList(true);
        refresh.setOnRefreshListener(refreshLayout -> getForumList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getForumList(false));

        KeyboardUtils.addKeyboardToggleListener(this, new KeyboardUtils.SoftKeyboardToggleListener() {
            @Override
            public void onToggleSoftKeyboard(boolean isVisible) {
                if (isVisible){

                }else {
                    if (dynamicInputTextMsgDialog != null){
                        dynamicInputTextMsgDialog.dismiss();
                    }
                }
            }
        });

        /*mDynamicAdapter.setReplyListener(new DynamicAdapter.ReplyListener() {
            @Override
            public void commentReplyUser(String item, String string,int position) {
                forum_id = item;
                forumCommentID = string;
                if (forumCommentID.equals("dian")){
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("ForumID", forum_id);
                    HttpClient.get(MyFabuActivity.this, Constants.GETFORUMLIKES, params,new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean result) {
                            getForumList((true));
                        }
                    });
                }else {
                    dynamicInputTextMsgDialog.setShowType();
                }
            }
        });*/
    }

    private void initInput() {
        dynamicInputTextMsgDialog = new DynamicInputTextMsgDialog(this);

       /* dynamicInputTextMsgDialog.setOnSendClickListener(new DynamicInputTextMsgDialog.OnSendClickListener() {
            @Override
            public void onSendClick(String text) {
                comment(text);
            }
        });*/
    }

    private void comment(String text) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("ForumID", forum_id);
        params.put("Content", text);
        params.put("ForumCommentID", forumCommentID);
        HttpClient.post2(this, Constants.POSTADDFORUMCOMMENT, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                getForumList(true);
            }
        });
    }

    private void getForumList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("SomeOneUserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(this, Constants.GETFORUMLIST, params,new CallBack<DynaminBean>() {
            @Override
            public void onSuccess(DynaminBean result) {
                list = result.getForumList();
                if (isRefresh){
                    refresh.finishRefresh(true);
                    if (list.size() == 0){
                        showEmpty(true,vsEmpty,2);
                    }else {
                        showEmpty(false, vsEmpty,2);
                    }
                    mDynamicAdapter.replaceAll(list, true);
                }else {
                    if (list.size() > 0) {
                        refresh.finishLoadMore(true);
                        mDynamicAdapter.addAll(list);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
                if (result.AllForumMessage == 0){
                    tvUnMsg.setVisibility(View.GONE);
                }else {
                    tvUnMsg.setVisibility(View.VISIBLE);
                    tvUnMsg.setText(result.AllForumMessage + "条新消息");
                }
            }
        });
    }
}
